#! /bin/bash
source .env
source ../common.sh

echo "${LINE_STR} commands available to users: "
echo "bash startup.sh: start task"
echo "bash startup.sh stop: stop task"
echo "bash startup.sh save: save image"
echo "bash startup.sh load: load image"
echo "bash startup.sh config: show config"

function start() {
  bash build.sh
  docker-compose down
  docker-compose up -d
#  docker rmi $(docker images -q -f dangling=true)
}

# 保存镜像
function imageSave() {
  echo "${LINE_STR} before save image"
  if [ -f "$PWD/image.tar.gz" ]; then
    rm -rf image.tar.gz
  fi
  docker save ${image_tag} | gzip > image.tar.gz
  echo "${LINE_STR} after save image"
}

# 加载镜像
function imageLoad() {
  echo "${LINE_STR} before load image"
  gunzip -c image.tar.gz | docker load
  echo "${LINE_STR} after load image"
}

echo "input command: $1"
if [ "$1" == "start" ]; then
  start 
elif [ "$1" == "save" ]; then
  imageSave
elif [ "$1" == "load" ]; then
  imageLoad
elif [ "$1" == "config" ]; then
  docker-compose config
else
  start
fi